package com.hwua.util;

import java.util.InputMismatchException;
import java.util.Scanner;

/**
 * 键盘录入的工具类
 * @author admin
 */
public class InputTools {
    private static Scanner scanner;
    static {
        scanner=new Scanner(System.in);
    }

    /**
     * 获取整型输入的方法
     */
    public static int checkIntInput(int begin,int end){
        int result=0;
        while (true){
            try{
                result=scanner.nextInt();
                if(result<begin||result>end){
                    System.out.println("没有该选项，请重新输入！");
                    continue;
                }
                break;
            }catch (InputMismatchException e){
                scanner.next();
                System.out.println("只可以输入数字，请重新输入：");
            }
        }
        return result;
    }
    /**
     * 获取浮点型输入的方法
     */
    public static double checkDoubleInput(double begin,double end){
        double result=0;
        while (true){
            try{
                result=scanner.nextDouble();
                if(result<begin||result>end){
                    System.out.println("超出范围，请重新输入！");
                    continue;
                }
                break;
            }catch (InputMismatchException e){
                scanner.next();
                System.out.println("只可以输入数字，请重新输入：");
            }
        }
        return result;
    }
    /**
     * 获取字符串输入
     * @return
     */
    public static String nextString(){
        return scanner.next();
    }
    /**
     * 关闭Scanner
     */
    public static void closeScanner(){
        scanner.close();
    }
}
